<?php

  class Controller_Upload extends Controller_Base {


	function index($args) {

		$error = "";
		$msg = "";
		$fileElementName = 'fileToUpload';
		if(!empty($_FILES[$fileElementName]['error'])) {
			switch($_FILES[$fileElementName]['error']) {
	
				case '1':
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2':
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3':
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$error = 'No file was uploaded.';
					break;
	
				case '6':
					$error = 'Missing a temporary folder';
					break;
				case '7':
					$error = 'Failed to write file to disk';
					break;
				case '8':
					$error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$error = 'No error code avaiable';
			}
		} elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none') {
			$error = 'No file was uploaded..';
		} else {
			
				$sub_dir = $args[0] === 'img' ? 'upload/' : 'tmp';
		
				$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
				$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
				$msg .= " TMP: ".$_FILES['fileToUpload']['tmp_name'];
				//for security reason, we force to remove all uploaded file
				$new_file = site_path.'static/userfiles/Images/'.$sub_dir.$_FILES['fileToUpload']['name'];
				error_log("file uploaded to: $new_file");
				@copy($_FILES['fileToUpload']['tmp_name'], $new_file);
				//@chmod(site_path.'userfiles/Image/'.$sub_dir.$_FILES['fileToUpload']['name'], 777);
				@unlink($_FILES['fileToUpload']);	
					
		}		
		echo "{";
		echo				"error: '" . $error . "',\n";
		echo				"msg: '/userfiles/Images/$sub_dir".$_FILES['fileToUpload']['name']."'\n";
	//	echo				"msg: '" . $msg . "'\n";
	//	echo				"filename: 'image/".$_FILES['fileToUpload']['name']."'\n";
		echo "}";

	}
  }
?>